package cn.edu.lingnan.mapper;

import cn.edu.lingnan.user.dtos.ListExamDto;
import cn.edu.lingnan.user.dtos.ListUserExam;
import cn.edu.lingnan.user.dtos.SubmitExamDTO;
import cn.edu.lingnan.user.pojos.Exam;
import cn.edu.lingnan.user.pojos.User;
import cn.edu.lingnan.user.vos.UserVO;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface ExamMapper {
    Page<Exam> getExamList(ListExamDto listExamDto);

    void addExam(Exam exam);

    @Delete("delete from tb_exam_system.t_user_exam where exam_id = #{examId}")
    void deleteUserExam(Integer examId);

    @Delete("delete from tb_exam_system.t_exam where exam_id = #{examId}")
    void deleteExam(Integer examId);

    @Select("select status from tb_exam_system.t_exam where exam_id = #{examId}")
    Integer getExamStatus(Integer examId);

    Page<UserVO> getExamUser(ListUserExam listUserExam);

    @Select("select count(*) from tb_exam_system.t_user_exam where exam_id = #{examId} ")
    Integer getExamCount(Integer examId);

    @Select("select * from tb_exam_system.t_exam where exam_id = #{examId}")
    Exam getExamById(Integer examId);

    void updateExam(Exam exam);

    @Insert("insert into tb_exam_system.t_user_exam(user_id,exam_id,status) values(#{userId},#{examId},0)")
    void addUserExam(Integer userId, Integer examId);

    @Update("update tb_exam_system.t_exam set status = #{status} where exam_id = #{examId}")
    void updateExamStatus(Integer examId, int status);

    @Select("select user_id from tb_exam_system.t_user_exam where exam_id = #{examId}")
    List<Integer> getExamUserIdList(Integer examId);

    @Select("select  count(*) from tb_exam_system.t_exam where paper_id = #{paperId}")
    Integer getExamIdByPaperId(Integer paperId);

    Page<Exam> getMyExamList(ListExamDto listExamDto);


    @Select("select  status from tb_exam_system.t_user_exam where user_id =#{userId} and exam_id = #{examId}")
    Integer getExamUserStatus(Integer userId, Integer examId);

    void updateExamMnultifileStatus(LocalDateTime startTime, LocalDateTime endTime, Integer status, Integer examId);

    void updteExamUserStatus(Integer examId, Integer currentId, Integer status);


    void processExamSubmission(SubmitExamDTO submitExamDTO);

    /**
     * 更新未提交的考试状态
     * @return
     */
    int updateUnsubmittedExams();

    @Select("SELECT COUNT(*) FROM t_user_exam WHERE user_id = #{userId}")
    int countExamByUserId(Integer userId);
}
